package com.idle.view;



import com.idle.android.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class CollapsibleTextView extends LinearLayout implements
        OnClickListener {

    /** default text show max lines */
    private static int DEFAULT_MAX_LINE_COUNT = 5;

    private static final int COLLAPSIBLE_STATE_NONE = 0;
    private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;
    private static final int COLLAPSIBLE_STATE_SPREAD = 2;

    private TextView desc;
    private TextView descOp;
    private TextView tvAdditional;

    private String shrinkup;
    private String spread;
    private int mState;
    private boolean flag;

    public CollapsibleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        shrinkup = context.getString(R.string.clpstv_desc_shrinkup);
        spread = context.getString(R.string.clpstv_desc_spread);
        View view = inflate(context, R.layout.collapsible_textview, this);
        view.setPadding(0, -1, 0, 0);
        desc = (TextView) view.findViewById(R.id.desc_tv);
        descOp = (TextView) view.findViewById(R.id.desc_op_tv);
        tvAdditional = (TextView) view.findViewById(R.id.tvAdditional);
        descOp.setOnClickListener(this);
        if (attrs != null) {
        	initAttrs(context, attrs);
		}
    }

    public CollapsibleTextView(Context context) {
        this(context, null);
    }
    
    private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.CollapsibleTextView, 0, 0);
		float descTextSize = typeArray.getDimension(R.styleable.CollapsibleTextView_descTextSize, 28);
		float descOpTextSize = typeArray.getDimension(R.styleable.CollapsibleTextView_descOpTextSize, 28);
		int descTextColor = typeArray.getColor(R.styleable.CollapsibleTextView_descTextColor, 0xFF000000);
		int descOpTextColor = typeArray.getColor(R.styleable.CollapsibleTextView_descOpTextColor, 0xFF576b95);
		int descMaxLines = typeArray.getInt(R.styleable.CollapsibleTextView_descMaxLines, 5);
		CharSequence descOpSpreadText = typeArray.getText(R.styleable.CollapsibleTextView_descOpSpreadText);
		CharSequence descOpShrinkupText = typeArray.getText(R.styleable.CollapsibleTextView_descOpShrinkupText);
		
		typeArray.recycle();
		
		desc.setTextSize(TypedValue.COMPLEX_UNIT_PX, descTextSize);
		descOp.setTextSize(TypedValue.COMPLEX_UNIT_PX, descOpTextSize);
		
		desc.setTextColor(descTextColor);
		descOp.setTextColor(descOpTextColor);
		DEFAULT_MAX_LINE_COUNT = descMaxLines;
		if (descOpShrinkupText != null) {
			shrinkup = descOpShrinkupText.toString();
		}
		if (descOpSpreadText != null) {
			spread = descOpSpreadText.toString();
		}
	}

    public final void setText(CharSequence charSequence, BufferType bufferType) {
        desc.setText(charSequence, bufferType);
        mState = COLLAPSIBLE_STATE_SPREAD;
        if (charSequence.toString().equals("")) {
        	desc.setVisibility(View.GONE);
		}else {
			desc.setVisibility(View.VISIBLE);
		}
        requestLayout();
    }
    
	public final void setText(CharSequence charSequence) {
        desc.setText(charSequence);
        mState = COLLAPSIBLE_STATE_SPREAD;
        if (charSequence.toString().equals("")) {
        	desc.setVisibility(View.GONE);
		}else {
			desc.setVisibility(View.VISIBLE);
		}
        requestLayout();
    }
	
	public TextView getAdditionalTextView() {
		return tvAdditional;
	}

    @Override
    public void onClick(View v) {
        flag = false;
        requestLayout();
    }

    public void resetDefaultState(){
    	flag = false;
    	mState = COLLAPSIBLE_STATE_SPREAD;
        requestLayout();
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!flag) {
            flag = true;
            if (desc.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {
                mState = COLLAPSIBLE_STATE_NONE;
                descOp.setVisibility(View.GONE);
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
                invalidate();
            } else {
                post(new InnerRunnable());
            }
        }
        requestLayout();
    }

    class InnerRunnable implements Runnable {
        @Override
        public void run() {
            if (mState == COLLAPSIBLE_STATE_SPREAD) {
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(spread);
                mState = COLLAPSIBLE_STATE_SHRINKUP;
            } else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
                desc.setMaxLines(Integer.MAX_VALUE);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(shrinkup);
                mState = COLLAPSIBLE_STATE_SPREAD;
            }
        }
    }
}
